package com.example.birdfriends.controller;

import com.example.birdfriends.dao.CommentDao;
import com.example.birdfriends.pojo.comment.Comment;
import com.example.birdfriends.pojo.comment.CommentFront;
import com.example.birdfriends.pojo.comment.CommentsAndAuthors;
import com.example.birdfriends.pojo.post.PostFront;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.service.CommentService;
import com.example.birdfriends.service.UserService;
import com.example.birdfriends.utils.convert.ConvertService;
import com.example.birdfriends.utils.result.Result;
import com.github.binarywang.java.emoji.EmojiConverter;
import com.vdurmont.emoji.Emoji;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService = null;
    @Autowired
    UserService userService = null;
    @Autowired
    private Result result = null;
    @Autowired
    private ConvertService convertService = null;

    private EmojiConverter emojiConverter = EmojiConverter.getInstance();

    @PostMapping("/create")  // 发表评论
    public Map<String, Object> postComment(@RequestBody Comment comment){
        // 前端传输的comment已有数据：文本内容，发布者id，所评论的帖子id，服务器需要设置评论发表的时间
        // 注：一个用户可以评论一个帖子多次
        System.out.println("发表评论");
        Long pid = comment.getPid();
        comment.setCreateAt(new Timestamp(System.currentTimeMillis()));
//        System.out.print("发表的评论：");comment.printComment();

        int insert = commentService.addComment(comment);
        boolean success = insert > 0;

        List<Comment> comments = commentService.findCommentsByPid(pid);
        List<User> authors = new ArrayList<>();
        List<CommentFront> commentFronts = new ArrayList<>();
        // 遍历comments，获取每个comment的uid，找到该评论的作者，并将user信息加入到users
        for(Comment comment1: comments){
            Long uid = comment1.getUid();
            commentFronts.add(convertService.comment2CommentFront(comment1));  // 将实体类的comment转化成前端使用的commentFront，并加入列表
            User commentAuthor = userService.findUserByUid(uid);
            commentAuthor.setPassword("");
            authors.add(commentAuthor);
        }

        CommentsAndAuthors commentsAndAuthors = new CommentsAndAuthors();
        commentsAndAuthors.setAuthorList(authors);
        commentsAndAuthors.setCommentList(commentFronts);

        if (success) {
            return result.resultMap(true, "评论成功", commentsAndAuthors);
        }
        return result.resultMap(false, "评论失败", commentsAndAuthors);
    }

    @PostMapping("/postComments")
    public Map<String, Object> queryPostComments(@RequestBody PostFront postFront){
        // 点击帖子详情时，用户应该同时可以获取对该帖子的所有评论
        Long pid = postFront.getPid();
        List<Comment> comments = commentService.findCommentsByPid(pid);
        boolean success = comments != null && comments.size() > 0;

        if (success){
            CommentsAndAuthors commentsAndAuthors = convertService.comments2CommentsAndAuthors(comments);  // 接帖子的所有评论转化成帖子评论和作者
            return result.resultMap(true, "检索评论数据成功", commentsAndAuthors);
        }

        return result.resultMap(false, "暂无评论数据", null);
    }
}
